package de.mhu.mandalaui.actions;

import java.io.PrintWriter;

import de.mhu.mandalaui.attributes.ArrayAttribute;
import de.mhu.mandalaui.bind.NamedDataRow;

public class FillArrayAttributeAction extends Action {

	private ArrayAttribute target;
	private boolean append = false;
	
	public FillArrayAttributeAction() {}
	
	public FillArrayAttributeAction(ArrayAttribute target) {
		setTarget(target);
	}
	
	@Override
	public void renderFunctionImplementation(PrintWriter out) {
		out.println("function(data) {");
		if (target != null) {
			if (!append) target.renderRemoveAll(out);
			out.println("  if (data) {");
			out.println("  for (var i in data) {");
		    target.renderAppendData(out, new NamedDataRow("data[i]") );
			out.println("  } }");
		}
		out.println("}");
	}

	public ArrayAttribute getTarget() {
		return target;
	}

	public void setTarget(ArrayAttribute target) {
		this.target = target;
	}

	public boolean isAppend() {
		return append;
	}

	public void setAppend(boolean append) {
		this.append = append;
	}

}
